package com.tedu.bill.demo8;

/**
 * Author：毕龙
 * Date: 2024/5/26 9:09
 */
public class MyThread extends Thread{
    //共享的对象
    private static String spoon = "勺子";
    private static String chopsticks = "筷子";

    @Override
    public void run() {
        while (true){
            if("小华".equals(getName())){
                synchronized (spoon){
                    System.out.println(getName()+"用"+spoon+"喝汤");
                    synchronized (chopsticks){
                        System.out.println(getName()+"用"+chopsticks+"吃面条");
                    }
                }
            }else{
                synchronized (chopsticks){
                    System.out.println(getName()+"用"+chopsticks+"吃面条");
                    synchronized (spoon) {
                        System.out.println(getName() + "用" + spoon + "喝汤");
                    }
                }
            }
        }
    }
}
